#include "TcpServer.hpp"
#include "Common.hpp"
#include "Protocol.hpp"
#include "Calculator.hpp"
#include "Parse.hpp"

using namespace wxd;
void Usage(char* argv[])
{
    std::cerr<<"usage: ./"<<argv[0]<<" port"<<std::endl;
}


void Debug()
{
    //序列化测试
    Request req(1,2,'+');
    std::string ret;
    req.Serialize(ret);
    std::cout<<"ret:\n"<<ret<<std::endl;
    //encode测试
    Encode(ret);
    std::cout<<"ret encoded:\n"<<ret<<std::endl;

    //decode测试
    std::string newret;
    Decode(ret,newret);
    std::cout<<"ret decoded:\n"<<newret<<std::endl;

    Request newreq;
    newreq.Deserialize(newret);
    newreq.PrintDebug();
}
int main(int argc,char* argv[])
{   
    
    //debug
    //Debug();
    

    //./TcpServer port
    if(argc != 2)
    {
        Usage(argv);
        exit(USAGE_ERR);
    }

    uint16_t server_port = std::stoi(argv[1]);

    //创建Cal对象
    std::shared_ptr<Calculator> ptr_calculator = std::make_shared<Calculator>();
    //绑定Cal的方法，用于传给Parse
    auto func_cal = std::bind(&Calculator::Excute,ptr_calculator,std::placeholders::_1);
    //创建Parse对象
    std::unique_ptr<Parse> ptr_parse = std::make_unique<Parse>(func_cal);
    //创建TcpServer对象
    //方法一：传入lambda表达式
    auto func_parse = [&ptr_parse](std::string& message){
        return ptr_parse->ExecuteParse(message);
    };
    std::unique_ptr<TcpServer> ptr_tcp_server = std::make_unique<TcpServer>(server_port,func_parse);
    

    ptr_tcp_server->Init();
    ptr_tcp_server->Start();
    
    return 0;
}